package com.sylar.happyreader.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sylar.happyreader.R;
import com.sylar.happyreader.bean.FileItem;

public class FileListAdapter extends BaseAdapter {
	ArrayList<FileItem> items;
	LayoutInflater inflater;

	public FileListAdapter(Context context, ArrayList<FileItem> items) {
		this.inflater = LayoutInflater.from(context);
		this.items = items;
	}

	public void dataChanged(ArrayList<FileItem> items) {
		this.items = items;
		this.notifyDataSetChanged();
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item, null);
			holder = new ViewHolder();
			holder.ivFileIcon = (ImageView) convertView.findViewById(R.id.ivFileIcon);
			holder.tvFileName = (TextView) convertView.findViewById(R.id.tvFileName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		FileItem item = items.get(position);

		holder.ivFileIcon.setImageDrawable(item.getFileIcon());
		holder.tvFileName.setText(item.getFileName());
		return convertView;
	}

	class ViewHolder {
		ImageView ivFileIcon;
		TextView tvFileName;
	}

}
